package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequirePermission;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;


    @RequestMapping("/list")
    @RequirePermission(name = "员工查询",expression = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageResult<Employee> pageResult = employeeService.query(qo);
        List<Employee> employees = employeeService.listAll();
        /*for (Employee employee : employees) {
            employee.getId();
            employee.getName();
            employee.getSn();
        }*/
        List<Department> departments = departmentService.listAll();
        System.out.println(departments);
        model.addAttribute("departments",departments);
        model.addAttribute("pageResult",pageResult);
        return "employee/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequirePermission(name = "员工删除",expression = "employee:delete")
    public JsonResult delete(Long id){
            try {
                employeeService.delete(id);
                return new JsonResult();
            } catch (Exception e) {
                e.printStackTrace();
                return new JsonResult("删除失败",false);
            }

    }


    @RequestMapping("/input")
    @RequirePermission(name = "员工添加",expression = "employee:input")
    public String input(Long id,Model model){
        //查询所有的角色
        List<Role> roles = roleService.listAll();

        model.addAttribute("roles",roles);

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        System.out.println(id);
        if(id!=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee );
        }
        return "employee/input";
    }
    @RequestMapping("saveOrUpdate")
    @RequirePermission(name = "员工保存或修改",expression = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){

        //往中间表存入数据
        //为防止中间表关系数据多余   先删除中间表再存入

        if (employee.getId()==null){
            employeeService.save(employee,ids);
        }else{
            employeeService.update(employee,ids);
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd(){

        Employee currentUser = UserContext.getCurrentUser();

        if (currentUser!=null){
            return  "common/updatePwd";
        }

        return "redirect:login.html";
    }



    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword,String newPassword){

        Employee currentUser = UserContext.getCurrentUser();

        if (currentUser.getPassword().equals(oldPassword)){

            employeeService.updatePwd(currentUser,newPassword);

            return new JsonResult();

        }

        return new JsonResult("密码修改失败",false);
    }





}
